package com.service.gzp.impl;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.stp.StpUtil;
import com.dao.UserinfoDao;
import com.dao.UserinforoleDao;
import com.entity.Userinfo;
import com.entity.UserinfoExample;
import com.entity.UserinforoleKey;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.service.gzp.UserinfoService;
import com.util.gzp.AjaxResponse;
import com.util.gzp.mima.AesUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: UserServiceImpl
 * Package: com.service.gzp.impl
 *
 * @author: guanzepeng
 * @Date: 2021/4/5 17:42 星期一
 * @Description: 进行登录的校验业务层
 */
@Service
@Slf4j
public class UserinfoServiceImpl implements UserinfoService {

	@Autowired
	private UserinfoDao userinfoDao;

	@Autowired
	private UserinforoleDao userinforoleDao;

	/**
	 * 查询是否存在该用户
	 * @param uname 传入用户名
	 * @return 如果成功返回token如果失败,返回500
	 */
	@Override
	public AjaxResponse getUserLogin(String uname, String upass) {
		UserinfoExample userinfoExample = new UserinfoExample();
		UserinfoExample.Criteria criteria = userinfoExample.createCriteria();
		//不查询已删除的用户
		criteria.andIsdeleteEqualTo(false);
		//传入用户名
		criteria.andUnameEqualTo(uname);
		//传入密码
		criteria.andUpassEqualTo(upass);

		List<Userinfo> list = userinfoDao.selectByExample(userinfoExample);

		if(1 == list.size()) {
			//先注销
			StpUtil.logoutByLoginId(list.get(0).getUid());
			//将用户ID存储
			StpUtil.setLoginId(list.get(0).getUid());
			String tokenName = StpUtil.getTokenName();
			String tokenValue = StpUtil.getTokenValue();
			Map<String, String> token = new HashMap<>();
			token.put("tokenName", tokenName);
			token.put("tokenValue", tokenValue);
			return AjaxResponse.success(token);
		}else {
			return AjaxResponse.success("登录异常,用户名或密码错误!",500);
		}
	}

	/**
	 * 查询所有的用户,带分页,以及姓名的模糊查询
	 * @param uname 用户姓名
	 * @param pageNum 分页
	 * @param pageSize 分页
	 * @return 返回数据
	 */
	@Override
	public AjaxResponse getUserList(String uname, Integer pageNum, Integer pageSize) {
		UserinfoExample userinfoExample = new UserinfoExample();
		UserinfoExample.Criteria criteria = userinfoExample.createCriteria();
		if(uname != null && !"".equals(uname)) {
			//存入账号名
			criteria.andUnameLike("%" + uname + "%");
		}
		PageHelper.startPage(pageNum, pageSize);
		List<Userinfo> userinfos = userinfoDao.selectByExample(userinfoExample);
		PageInfo<Userinfo> list = new PageInfo<>(userinfos);
		return AjaxResponse.success(list);
	}

	/**
	 * 软删除用户数据
	 * @param flag 传入true为删除,false为保留
	 * @param uid 传入用户ID辨识用户
	 * @return 返回成功失败
	 */
	@Override
	public AjaxResponse deleteUser(boolean flag, Integer uid) {
		UserinfoExample userinfoExample = new UserinfoExample();
		UserinfoExample.Criteria criteria = userinfoExample.createCriteria();
		//传入用户ID
		criteria.andUidEqualTo(uid);
		//传入删除数据
		Userinfo userinfo = new Userinfo();
		userinfo.setIsdelete(flag);
		boolean b = userinfoDao.updateByExampleSelective(userinfo, userinfoExample) == 1;
		if(b) {
			return AjaxResponse.success();
		}
		return AjaxResponse.success("删除失败!",500);
	}

	/**
	 * 修改用户密码
	 * @param uid 传入用户ID辨识用户
	 * @param pwd 传入密码
	 * @param newPwd 传入新密码
	 * @return 返回成功失败
	 */
	@Transactional
	@Override
	public AjaxResponse updatePwd(Integer uid, String pwd,  String newPwd) {
		//比对旧密码是否正确
		UserinfoExample userinfoExample2 = new UserinfoExample();
		UserinfoExample.Criteria criteria2 = userinfoExample2.createCriteria();
		//不查询已删除的用户
		criteria2.andIsdeleteEqualTo(false);
		//传入用户名
		criteria2.andUidEqualTo(uid);
		//传入密码
		criteria2.andUpassEqualTo(pwd);

		boolean b = userinfoDao.selectByExample(userinfoExample2).size() == 1;
		if(!b) {
			return AjaxResponse.success("旧密码输入错误!",500);
		}
		UserinfoExample userinfoExample = new UserinfoExample();
		UserinfoExample.Criteria criteria = userinfoExample.createCriteria();
		//传入用户ID
		criteria.andUidEqualTo(uid);	String encode = AesUtils.encode(newPwd);
		//传入密码
		Userinfo userinfo = new Userinfo();
		//加密密码
		userinfo.setUpass(encode);
		boolean b2 = userinfoDao.updateByExampleSelective(userinfo, userinfoExample) == 1;
		if(b) {
			return AjaxResponse.success();
		}
		return AjaxResponse.success("更改密码失败!",500);
	}

	/**
	 * 添加用户以及给用户授予权限
	 * @param userinfo 传入用户账号密码
	 * @param rid 传入权限ID
	 * @return 返回成功失败
	 */
	@Override
	public AjaxResponse insertUser(Userinfo userinfo, Integer rid) {
		//新增用户
		userinfo.setIsdelete(false);
		boolean b = userinfoDao.insertSelective(userinfo) == 1;
		if(!b) {
			return AjaxResponse.success("新增失败!",500);
		}
		//查询出新增用户的ID
		UserinfoExample userinfoExample = new UserinfoExample();
		UserinfoExample.Criteria criteria = userinfoExample.createCriteria();
		criteria.andUnameEqualTo(userinfo.getUname());
		criteria.andUpassEqualTo(userinfo.getUpass());
		criteria.andIsdeleteEqualTo(false);
		List<Userinfo> userinfos = userinfoDao.selectByExample(userinfoExample);
		Integer uid = userinfos.get(0).getUid();
		log.info("新增业务的uid为:" + uid);
		//新增角色
		UserinforoleKey userinforoleKey = new UserinforoleKey();
		userinforoleKey.setUid(uid);
		userinforoleKey.setRid(rid);
		b = userinforoleDao.insertSelective(userinforoleKey) == 1;
		if(!b) {
			return AjaxResponse.success("新增失败!",500);
		}
		return AjaxResponse.success();
	}

	/**
	 * 退出登录
	 * @return 返回成功失败
	 */
	@Override
	public AjaxResponse checkOut() {
		Integer uid = null;
		try {
			uid = StpUtil.getLoginIdAsInt();
		}catch (NotLoginException e) {
			return AjaxResponse.success("请先进行登录!",500);
		}
		StpUtil.logoutByLoginId(uid);
		return AjaxResponse.success();
	}

	/**
	 * 查询用户名
	 * @return 返回用户名
	 */
	@Override
	public AjaxResponse getName() {
		Map<String, String> map = new HashMap<>();
		Integer uid = null;
		try {
			uid = StpUtil.getLoginIdAsInt();
		}catch (NotLoginException e) {
			map.put("name","未登录");
			return AjaxResponse.success(map);
		}
		Userinfo userinfo = userinfoDao.selectByPrimaryKey(uid);
		String name = userinfo.getUname();
		map.put("name",name);
		return AjaxResponse.success(map);
	}
}
